#pragma once
#include <string>
#include"Init.hpp"
Init init;
const int buffersize = 1024;
class Task
{
private:
  int sock_fd;
  std::string clientip_;
  uint16_t clientport_;

public:
  Task(const int &fd, const std::string &ip, const uint16_t &port)
      : sock_fd(fd), clientip_(ip), clientport_(port)
  {
  }
  void operator()()
  {
    Run();
  }
  void Run()
  {

    char buffer[buffersize];
    ssize_t n = read(sock_fd, buffer, sizeof(buffer));
    if (n > 0)
    {
      buffer[n] = '\0';
      std::string message = buffer; // 获取client的信息
      std::string echo_message = init.translation(message);
      // 2.写
    // 把获取到的信息进行转换，输出client想要的信息
      ssize_t w = write(sock_fd, echo_message.c_str(), echo_message.size());
      if (w < 0)
      {
        lg(WARNING, "Write fail!!");
      }
      else if (w == 0)
      {
        lg(WARNING, "Read close!!");
      }
    }
    else if (n < 0)
    {
      lg(INFO, "Read fail!! client ip is:%s, clien port is:%d",clientip_.c_str(),clientport_);
    }
    else
    {
      lg(WARNING, "Client close!! fd:%d, client ip: %s, client port: %d",sock_fd,clientip_.c_str(),clientport_);
    }
    close(sock_fd);
  }
  ~Task()
  {
  }
};